#include "grptir.h"


#include <string.h>
#include <stdlib.h>
void initgrpTir(Grptir* G)
{
    G->ami=malloc(20*sizeof(Tir));
    G->ennemi=malloc(20*sizeof(Tir));
    G->tailleutiami=0;
    G->tailleennemi=0;
    G->tmaxami=20;
    G->tmaxennemi=20;
}

void detruiregrpTir(Grptir* G)
{
    free(G->ennemi);
    free(G->ami);
    G->tailleutiami=0;
    G->tailleennemi=0;
    G->tmaxami=0;
    G->tmaxennemi=0;
}

void detruireTirgrpTir(Grptir* G,int i,typetir type)
{
    int j;
    if (type==TIR_AMI)
    {
        detruireTir(&G->ami[i]);
        for (j=i;j<G->tailleutiami-1;j++)
            memcpy(&G->ami[j],&G->ami[j+1],sizeof(Tir));
        G->tailleutiami--;
        if ((G->tmaxami>20)&&(G->tmaxami-G->tailleutiami>20)) //Si il y a plus de 20 cases vides
        {
            G->ami=realloc(G->ami,(G->tmaxami-20)*sizeof(Tir));
            G->tmaxami-=20;
        }
    }
    if (type==TIR_ENNEMI)
    {
        detruireTir(&G->ennemi[i]);
        for (j=i;j<G->tailleennemi-1;j++)
            memcpy(&G->ennemi[j],&G->ennemi[j+1],sizeof(Tir));
        G->tailleennemi--;
        if ((G->tmaxennemi>20)&&(G->tmaxennemi-G->tailleennemi>20)) //Si il y a plus de 20 cases vides
        {
            G->ami=realloc(G->ami,(G->tmaxennemi-20)*sizeof(Tir));
            G->tmaxennemi-=20;
        }
    }
}

void deplacergrpTir(Grptir* G,float intervalle)
{
    int i=0;
    while (i<G->tailleutiami)
    {
        deplacerTir(&G->ami[i],intervalle);
        if (getPertevieTir(&G->ami[i])==0)
            detruireTirgrpTir(G,i,TIR_AMI);
        else
            i++;
    }
    i=0;
    while (i<G->tailleennemi)
    {
        deplacerTir(&G->ennemi[i],intervalle);
        if (getPertevieTir(&G->ennemi[i])==0)
            detruireTirgrpTir(G,i,TIR_ENNEMI);
        else
            i++;
    }
}

void nettoyergrpTir(Grptir* G)
{
    G->tailleutiami=0;
    G->tailleennemi=0;
}

void ajouterTirgrptir(Grptir* G,Tir *T,typetir type)
{
    if (type==TIR_AMI)
    {
        memcpy(&G->ami[G->tailleutiami],T,sizeof(Tir));
        G->tailleutiami++;
        if (G->tailleutiami==G->tmaxami)
        {
            G->ami=realloc(G->ami,(G->tmaxami+20)*sizeof(Tir));
            G->tmaxami+=20;
        }

    }
    if (type==TIR_ENNEMI)
    {
        memcpy(&G->ennemi[G->tailleennemi],T,sizeof(Tir));
        G->tailleennemi++;
        if (G->tailleennemi==G->tmaxennemi)
        {
            G->ennemi=realloc(G->ennemi,(G->tmaxennemi+20)*sizeof(Tir));
            G->tmaxennemi+=20;
        }
    }
}

void setTempsGrptir(Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        setTempsTir(&T->ami[i]);
    if (type==TIR_ENNEMI)
        setTempsTir(&T->ennemi[i]);
}

void setx0y0Grptir(Grptir* T, double x0, double y0,int i,typetir type)
{
    if (type==TIR_AMI)
        setx0y0Tir(&T->ami[i],x0,y0);
    if (type==TIR_ENNEMI)
        setx0y0Tir(&T->ennemi[i],x0,y0);
}

void setRangeGrptir(Grptir* T, int distance,int i,typetir type)
{
    if (type==TIR_AMI)
        setRangeTir(&T->ami[i],distance);
    if (type==TIR_ENNEMI)
        setRangeTir(&T->ennemi[i],distance);
}

void setAngleGrptir(Grptir* T, float rad,int i,typetir type)
{
    if (type==TIR_AMI)
        setAngleTir(&T->ami[i],rad);
    if (type==TIR_ENNEMI)
        setAngleTir(&T->ennemi[i],rad);
}

void setPertevieGrptir(Grptir* T, int pertevie,int i,typetir type)
{
    if (type==TIR_AMI)
        setPertevieTir(&T->ami[i],pertevie);
    if (type==TIR_ENNEMI)
        setPertevieTir(&T->ennemi[i],pertevie);
}


void setxyGrptir(Grptir* T, int x, int y,int i,typetir type)
{
    if (type==TIR_AMI)
        setxyTir(&T->ami[i],x,y);
    if (type==TIR_ENNEMI)
        setxyTir(&T->ennemi[i],x,y);
}

void setidGrptir(Grptir* T,int i,typetir type,int id)
{
    if (type==TIR_AMI)
        setidTir(&T->ami[i],id);
    if (type==TIR_ENNEMI)
        setidTir(&T->ennemi[i],id);
}

float getTempsGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getTempsTir(&T->ami[i]);
    return getTempsTir(&T->ennemi[i]);
}

int getx0Grptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getx0Tir(&T->ami[i]);
    return getx0Tir(&T->ennemi[i]);
}

int gety0Grptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return gety0Tir(&T->ami[i]);
    return gety0Tir(&T->ennemi[i]);
}

int getRangeGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getRangeTir(&T->ami[i]);
    return getRangeTir(&T->ennemi[i]);
}

float getAngleGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getAngleTir(&T->ami[i]);
    return getAngleTir(&T->ennemi[i]);
}

int getPertevieGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getPertevieTir(&T->ami[i]);
    return getPertevieTir(&T->ennemi[i]);
}

double getxdoubleGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getxdoubleTir(&T->ami[i]);
    return getxdoubleTir(&T->ennemi[i]);
}

double getydoubleGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getydoubleTir(&T->ami[i]);
    return getydoubleTir(&T->ennemi[i]);
}

void redemmarerGrptir(Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        redemmarerTir(&T->ami[i]);
    if (type==TIR_ENNEMI)
        redemmarerTir(&T->ennemi[i]);
}

int getTailleUtiGrptir(const Grptir* T,typetir type)
{
    if (type==TIR_AMI)
        return T->tailleutiami;
    return T->tailleennemi;
}

int getxGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getxTir(&T->ami[i]);
    return getxTir(&T->ennemi[i]);
}

int getyGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getyTir(&T->ami[i]);
    return getyTir(&T->ennemi[i]);
}

int getidGrptir(const Grptir* T,int i,typetir type)
{
    if (type==TIR_AMI)
        return getidTir(&T->ami[i]);
    return getidTir(&T->ennemi[i]);
}
